import App from './App.vue'
import { createApp } from 'vue'
import NProgress from 'nprogress'
import 'element-plus/dist/index.css'
import { routers } from '@/router'
import { createPinia } from 'pinia'
import 'nprogress/nprogress.css'
import routerStore from '@/store/router-store'

const app = createApp(App)
app.use(routers)
app.use(createPinia())


const localRouter = routerStore()

routers.beforeEach((to, from, next) => {

  Object.assign(to.params, {
    role: getPageRole(to.path)
  })

  if (to.meta.parentName) {

    localRouter.RECORD_COMONENT({
      title: <string>to.meta.title,
      name: <string>to.name,
      parentName: to.meta.parentName,
      path: to.path
    })
  }

  NProgress.start()
  next()
})

routers.afterEach(() => {
  NProgress.done()
})

app.mount('#app')

// 通过path匹配当前模块按钮级权限
function getPageRole(str: string) {
  if (str.length === 0) { return undefined }
  const baseKey = str.split('/').slice(1)
  baseKey[0] = '/' + baseKey[0]

  // eslint-disable-next-line
  let flag: any = localRouter.GET_USER_ROLE
  while(true) {
    const localKey = baseKey.shift()
    if (!localKey) break
    flag = flag[localKey]
    if (!flag || Array.isArray(flag)) break
  }
  return flag
}
